/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.DAOs;

import DBConnection.ConnectionFactory;
import Models.Audio;
import Models.Products;
import Security.Encrypter;
import java.sql.*;
import java.util.ArrayList;
/**
 *
 * @author 0905
 */
public class Audio_DAO extends Product_DAO {
    
    public boolean addAudio(Audio audio){
        try{
            
            int productID = this.addProduct(audio);
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            
            PreparedStatement pstmtAudio = conn.prepareStatement("INSERT INTO audio(recordingArtist, length, genre, label, producer, productID) VALUES(?,?,?,?,?,?)");           
            pstmtAudio.setString(i++, audio.getRecordingArtist());
            pstmtAudio.setString(i++, audio.getLength());
            pstmtAudio.setString(i++, audio.getGenre());
            pstmtAudio.setString(i++, audio.getLabel());
            pstmtAudio.setString(i++, audio.getProducer());
            pstmtAudio.setInt(i++, productID);

            pstmtAudio.executeUpdate();
            return true;
        }
        catch(Exception e){
            return false;
        }
    }
    
    public boolean updateAudio(Audio audio, String audioCode){
        try{
            String encryptedAudio = new Encrypter().decrypt(audioCode);
            audio.setProductID(Integer.parseInt(encryptedAudio));
            
            boolean updatedProduct = this.updateProduct(audio);
            
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int i = 1;
            PreparedStatement pstmtAudio = conn.prepareStatement("UPDATE audio SET recordingArtist = ?, length = ?, genre = ?, label = ?, producer = ? WHERE audioID = ?)");
           
            pstmtAudio.setString(i++, audio.getRecordingArtist());
            pstmtAudio.setString(i++, audio.getLength());
            pstmtAudio.setString(i++, audio.getGenre());
            pstmtAudio.setString(i++, audio.getLabel());
            pstmtAudio.setString(i++, audio.getProducer());
            pstmtAudio.setInt(i++, audio.getAudioID());

            pstmtAudio.executeUpdate();
            
            return true;
        }
        catch(Exception e){
            
            return false;
        }
    }
    public ArrayList<Audio> getAllAudio(){
        
        Audio audio;
        ArrayList<Audio> audioList = null;
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            CallableStatement pstmt = conn.prepareCall("{ call GET_ALL_AUDIO() }");
            ResultSet rs = pstmt.executeQuery();
            audioList = new ArrayList<Audio>();
            Encrypter encrypt = new Encrypter();
            while(rs.next())
            {
                audio = new Audio();
                
                //System.out.println(encrypt.encrypt());
                audio.setProductCrypt(encrypt.encrypt(Integer.toString(rs.getInt("productID"))));
                //String str  = Integer.toString(rs.getInt("prodcutID"));
                //System.out.println(str);
                
                audio.setProductID(rs.getInt("productID"));
                audio.setAudioID(rs.getInt("audioID"));
                audio.setTitle(rs.getString("title"));
                audio.setRecordingArtist(rs.getString("recordingArtist"));
                audio.setPrice(rs.getFloat("price"));
                audio.setSynopsis(rs.getString("synopsis"));
                audio.setLength(rs.getString("length"));
                audio.setGenre(rs.getString("genre"));
                audio.setLabel(rs.getString("label"));
                audio.setProducer(rs.getString("producer"));
                audioList.add(audio);
            }
            
            return audioList;
        }
        catch(SQLException e){
        
            return audioList;
        }
            
    }
    
    public Audio getAudio(int audioID){
        
        Audio audio = null;
        
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM audio b INNER JOIN products p ON b.productID = p.productID WHERE b.productID = ?");
            pstmt.setInt(1, audioID);
            
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                audio = new Audio();
                audio.setProductCrypt(new Encrypter().encrypt(Integer.toString(rs.getInt("productID"))));
                audio.setAudioID(rs.getInt("audioID"));
                audio.setTitle(rs.getString("title"));
                audio.setRecordingArtist(rs.getString("recordingArtist"));
                audio.setPrice(rs.getFloat("price"));
                audio.setSynopsis(rs.getString("synopsis"));
                audio.setLength(rs.getString("length"));
                audio.setGenre(rs.getString("genre"));
                audio.setLabel(rs.getString("label"));
                audio.setProducer(rs.getString("producer"));
                
            }
            return audio;
        }
        catch(SQLException e){
        
            e.printStackTrace();
            return audio;
        }
    }
}
